/**
 * @projectName: java-daily-questions
 * @package: PACKAGE_NAME
 * @className: SequentialExecution
 * @author: ZM
 * @description: 顺序执行 - 打印abc
 * @date: 2021/8/22 18:04
 * @version: 1.0
 */
public class SequentialExecution {
    
    private static boolean flagA = true;
    private static boolean flagB = false;
    private static boolean flagC = false;

    public static void main(String[] args) {
        final Object obj = new Object();
        
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (obj) {
                    while (!flagA) {
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print("A");
                    flagA = false;
                    flagB = true;
                    flagC = false;
                    obj.notifyAll();
                }
            }
        }).start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (obj) {
                    while (!flagB) {
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print("B");
                    flagA = false;
                    flagB = false;
                    flagC = true;
                    obj.notifyAll();
                }
            }
        }).start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (obj) {
                    while (!flagC) {
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print("C");
                    flagA = true;
                    flagB = false;
                    flagC = false;
                    obj.notifyAll();
                }
            }
        }).start();

    }
    
}
